/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package issr.boundary;

import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Form;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Select;
import com.vaadin.ui.TextField;
import issr.boundary.vaadin.AbstractView;
import issr.boundary.vaadin.MainApplication;
import issr.controller.ControlloreCreazioneDipendenteGestorePV;
import issr.entity.Ruolo;
import issr.entity.Utente;
import issr.misc.Labels;
import java.util.ArrayList;

/**
 *
 * @author Soulreaver
 */
public class PannelloCreazioneDipendenteGestorePV extends AbstractView implements Button.ClickListener {

    private ControlloreCreazioneDipendenteGestorePV controllore;
    private final TextField nomeField = new TextField(Labels.NOME);
    private final TextField cognomeField = new TextField(Labels.COGNOME);
    private final TextField loginField = new TextField(Labels.LOGIN);
    private final TextField passwordField = new TextField(Labels.PASSWORD);
    private final TextField emailField = new TextField(Labels.EMAIL);
    private final TextField cellulareField = new TextField(Labels.CELLULARE);
    private final Select ruoloSelect = new Select(Labels.RUOLO);
    private final Button submit;
    private final Form form = new Form();
    private final FormLayout layout;

    public PannelloCreazioneDipendenteGestorePV() {
        controllore = new ControlloreCreazioneDipendenteGestorePV();
        layout = new FormLayout();
        // Create the visual components of the view.
        setSizeFull();
        setCompositionRoot(layout);
        layout.setHeight("100%");
        layout.setWidth("100%");
        layout.setSpacing(true);
        layout.setMargin(true);

        nomeField.setRequired(true);
        cognomeField.setRequired(true);
        loginField.setRequired(true);
        passwordField.setRequired(true);
        ruoloSelect.setRequired(true);

        form.addField(Labels.NOME, nomeField);
        form.addField(Labels.COGNOME, cognomeField);
        form.addField(Labels.LOGIN, loginField);
        form.addField(Labels.PASSWORD, passwordField);
        form.addField(Labels.EMAIL, emailField);
        form.addField(Labels.CELLULARE, cellulareField);
        form.addField(Labels.RUOLO, ruoloSelect);

        layout.addComponent(form);
        submit = new Button("Submit", this);
        submit.setClickShortcut(KeyCode.ENTER);
        layout.addComponent(submit);
        setCompositionRoot(layout);
    }

    @Override
    protected void close() throws Exception {
    }

    @Override
    protected void open() throws Exception {
        visualizzaForm();
    }

    protected void visualizzaForm() {
        resetForm();
        ArrayList<Ruolo> ruoli = controllore.getListaRuoli();
        for (Ruolo role : ruoli) {
            ruoloSelect.addItem(role);
        }
    }

    protected void resetForm() {
        nomeField.setValue("");
        cognomeField.setValue("");
        loginField.setValue("");
        passwordField.setValue("");
        emailField.setValue("");
        cellulareField.setValue("");
        ruoloSelect.setValue("");
        ruoloSelect.removeAllItems();
    }

    @Override
    public void buttonClick(ClickEvent event) {
        if (controllore.creaNuovoDipendente(
                (String) nomeField.getValue(),
                (String) cognomeField.getValue(),
                (String) loginField.getValue(),
                (String) passwordField.getValue(),
                (String) emailField.getValue(),
                (String) cellulareField.getValue(),
                (Ruolo) ruoloSelect.getValue(),
                (Utente) MainApplication.getProject().getUser()) == true) {
            MainApplication.getProject().getMainWindow().showNotification(
                    "Utente " + (String) loginField.getValue() + " creato con successo");
        } else {
            MainApplication.getProject().getMainWindow().showNotification(
                    "Errore creazione dipendente");
        }
    }
}
